/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.server;


import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gwtportlets.portlet.client.WidgetFactory;
import org.gwtportlets.portlet.server.PageRequest;
import za.org.foobar.famon.frontend.client.FamonException;
import za.org.foobar.famon.frontend.client.FamonPage;
import za.org.foobar.famon.frontend.client.FamonService;
import za.org.foobar.famon.frontend.client.dto.ApplicationDto;
import za.org.foobar.famon.frontend.client.dto.CModelObjectSuggestion;
import za.org.foobar.famon.util.DB;


/**
 * Implementation of our remote service interface.
 */
public class FamonServiceImpl extends RemoteServiceServlet implements FamonService {
    private final Log log = LogFactory.getLog(getClass());

    public FamonServiceImpl() {
    }

    public FamonPage getRootPage(String historyToken) {
        FamonPage p = getPage(historyToken);
        PageRequest req = createPageRequest(historyToken);
        req.setPageName("root");
        p.rootWidgetFactory = FamonPageProvider.get().openPage(req);
        if (p.rootWidgetFactory == null) {
            throw new FamonException("No root page defined!");
        }
        return p;
    }

    private FamonPageRequest createPageRequest(String historyToken) {
        FamonPageRequest req = new FamonPageRequest(historyToken);
        req.setServletConfig(getServletConfig());
        req.setServletRequest(getThreadLocalRequest());
        req.setServletResponse(getThreadLocalResponse());
        return req;
    }

    public FamonPage getPage(String historyToken) {
        FamonPageRequest req = createPageRequest(historyToken);
        if (req.getPageName().length() == 0) {
            req.setPageName("home");
        }
        FamonPage p = new FamonPage();
        p.pageName = req.getPageName();
        p.widgetFactory = FamonPageProvider.get().openPage(req);
        p.canEditPage = true;
        p.paramMap = req.getParameterMap();
        if (p.paramMap.size() == 0) {
            p.paramMap = null;
        }

        UserLogic userLogic = UserLogic.get();
        p.security = userLogic.createSecurityDto();

        return p;
    }

    public void savePage(String pageName, WidgetFactory factory) {
        FamonPageProvider.get().savePage(pageName, factory);
    }

    public WidgetFactory refresh(String historyToken, WidgetFactory wf) {
        FamonPageProvider.get().refresh(createPageRequest(historyToken), wf);
        DB.closePM();
        return wf;
    }

    public void newPage(String page) throws FamonException {
        PageEditorLogic.get().newPage(page);
    }

    public String getPageXml(String page) throws FamonException {
        return PageEditorLogic.get().getPageXml(page);
    }

    public void setPageXml(String page, String value) throws FamonException {
        PageEditorLogic.get().setPageXml(page, value);
    }

    public CModelObjectSuggestion[] getModelObjects(
            String dbClassName, String dbValueField, String dbWhere,
            String query) throws FamonException {
        return ModelObjectLogic.get().getModelObjects(dbClassName, dbValueField,
                dbWhere, query);
    }

    public void updateApplication(ApplicationDto applicationDto) throws FamonException {
//        ApplicationLogic.get().updateApplication(applicationDto);
    }

    public void deleteApplication(String applicationId) throws FamonException {
        ApplicationLogic.get().deleteApplication(applicationId);
    }
}
